package com.suse.lhw.hms.authserver.questionnaire;

import lombok.Data;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @ProjectName: Algorithm
 * @Package: algorithm.LEETCODE
 * @ClassName: questionnaire
 * @Description: 用于问卷调查
 * @Author: 李鸿炜
 * @CreateDate: 2020/2/12 10:33
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/2/12 10:33
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class questionnaire {

    public UserMessage readFile() {
        /**
         * @Author:Hongwei Li
         * @Description:读取项目外的文件：userProperties.txt,并返回用户的信息
         * @Date:11:53 2020/2/12
         * @Param:[]
         * @Return:algorithm.LEETCODE.问卷调查.UserMessage
         **/
        BufferedReader bufferedReader = null;
        String jarWholePath = new File("").getAbsolutePath();
        UserMessage userMessage = new UserMessage();
        try {
            FileInputStream fileInputStream = new FileInputStream(jarWholePath + "/userProperties.txt");
            bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, "GBK"));
            //System.out.println(bufferedReader.readLine().split("：")[1]);
            userMessage.setTerm(bufferedReader.readLine().split("：")[1]);
            userMessage.setMajor(bufferedReader.readLine().split("：")[1]);
            userMessage.setClasss(bufferedReader.readLine().split("：")[1]);
            userMessage.setName(bufferedReader.readLine().split("：")[1]);
            userMessage.setNunber(bufferedReader.readLine().split("：")[1]);
            //    System.out.println(bufferedReader.readLine().split("：|,")[2]);
            String point = bufferedReader.readLine();
            userMessage.setFirst(Integer.parseInt(point.split("：|,")[1]));
            userMessage.setLast(Integer.parseInt(point.split("：|,")[2]));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return userMessage;
    }

    public static void accessURL() {
        /**
         * @Author:Hongwei Li
         * @Description:进入网站填写相关信息
         * @Date:11:54 2020/2/12
         * @Param:[userMessage]
         * @Return:void
         **/
        String site = "https://www.wjx.cn/jq/73975173.aspx";

        //    String site = "https://www.wjx.cn/jq/56992751.aspx";

        try {
            Desktop desktop = Desktop.getDesktop();
            if (desktop.isDesktopSupported()
                    && desktop.isSupported(Desktop.Action.BROWSE)) {
                URI uri = new URI(site);
                desktop.browse(uri);
            }
        } catch (IOException ex) {
            System.out.println(ex);
        } catch (URISyntaxException ex) {
            System.out.println(ex);
        }
    }

    public static void fill(UserMessage userMessage) {
        /**
         * @Author:Hongwei Li
         * @Description:实现填写表单
         * @Date:12:08 2020/2/12
         * @Param:[]
         * @Return:void
         **/
        try {
            Robot robot = new Robot();
            Thread.sleep(3000);
            //移动鼠标
            if (userMessage.getLast() == 0 && userMessage.getFirst() == 0)
                robot.mouseMove(420, 565);
                //   robot.mouseMove(426,454);
            else
                robot.mouseMove(userMessage.getFirst(), userMessage.getLast());
            //选择年级班级输入框
            Thread.sleep(300);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            Thread.sleep(100);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            Thread.sleep(100);
            //选择年级
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            Integer termNum = Integer.parseInt(userMessage.getTerm()) - 2015;
            Thread.sleep(100);
            for (int i = 0; i < termNum; i++) {
                robot.keyPress(KeyEvent.VK_DOWN);
                robot.keyRelease(KeyEvent.VK_DOWN);
                Thread.sleep(100);
            }

            //    Thread.sleep(100);
            //System.exit(0);
            //选择专业
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);

            robot.keyPress(KeyEvent.VK_DOWN);
            robot.keyRelease(KeyEvent.VK_DOWN);
            String[] stringsArray = {"计算机科学", "软件工程", "电子商务", "物联网工程", "网络工程"};
            int i;
            for (i = 0; i < stringsArray.length; i++) {
                if (userMessage.getMajor().equals(stringsArray[i]))
                    break;
                else {
                    robot.keyPress(KeyEvent.VK_DOWN);
                    robot.keyRelease(KeyEvent.VK_DOWN);
                    Thread.sleep(20);
                }

            }

            //选择班级
            Thread.sleep(100);
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            Thread.sleep(100);
            for (int j = 0; j < Integer.parseInt(userMessage.getClasss()); j++) {
                robot.keyPress(KeyEvent.VK_DOWN);
                robot.keyRelease(KeyEvent.VK_DOWN);
                Thread.sleep(70);
            }
//            robot.keyPress(KeyEvent.VK_ENTER);
//            robot.keyRelease(KeyEvent.VK_ENTER);
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            Thread.sleep(100);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
            //选择姓名
            Thread.sleep(100);
            for (int j = 0; j < 5; j++) {
                robot.keyPress(KeyEvent.VK_TAB);
                robot.keyRelease(KeyEvent.VK_TAB);
                Thread.sleep(10);
            }
            Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
            Transferable tText = new StringSelection(userMessage.getName());
            clip.setContents(tText, null);
            Thread.sleep(200);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            Thread.sleep(400);
            //写学号
            Thread.sleep(100);
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            Thread.sleep(100);
            Transferable tText1 = new StringSelection(userMessage.getNunber());
            clip.setContents(tText1, null);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            //写单选框
            //
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            Thread.sleep(100);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
            Thread.sleep(100);
            robot.keyPress(KeyEvent.VK_TAB);

            robot.keyRelease(KeyEvent.VK_TAB);
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            Thread.sleep(100);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
            //输入城市
            Thread.sleep(100);
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            Thread.sleep(100);
            Transferable tText11 = new StringSelection(userMessage.getAddress());
            clip.setContents(tText11, null);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);

            for (int j = 0; j < 3; j++) {
                robot.keyPress(KeyEvent.VK_TAB);
                robot.keyRelease(KeyEvent.VK_TAB);
                Thread.sleep(100);

            }
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
            Thread.sleep(200);

            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
            Thread.sleep(50);
            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
            Thread.sleep(50);
            for (int i1 = 0; i1 < 10; i1++) {
                robot.keyPress(KeyEvent.VK_DOWN);
                robot.keyRelease(KeyEvent.VK_DOWN);
            }

//            robot.keyPress(KeyEvent.VK_TAB);
//            robot.keyRelease(KeyEvent.VK_TAB);
//            Thread.sleep(100);
//            robot.keyPress(KeyEvent.VK_ENTER);
//            robot.keyRelease(KeyEvent.VK_ENTER);
        } catch (AWTException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        //questionnaire qq=new questionnaire();
        //   UserMessage userMessage=qq.readFile();

        accessURL();
        UserMessage userMessage = new UserMessage();
        fill(userMessage);
//https://www.wjx.cn/wjx/join/complete.aspx?q=56456759&JoinID=104030135656&jidx=6957&s=&rname=%E6%92%92%E8%80%8C%E9%9D%9E&jpm=13

    }
}
